package com.example.easeimpro.utils;

import android.Manifest;
import android.app.Activity;
import android.app.LauncherActivity;
import android.content.Intent;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.MediaStore;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.easeimpro.model.db.Constant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.utils.EaseCompat;
import com.hyphenate.util.PathUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 动态申请权限   工具类
 */
public class Permission {
    /**
     *     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     *     <!-- 允许应用程序改变任务的z顺序 -->
     *     <uses-permission android:name="android.permission.REORDER_TASKS"/>
     *     <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
     *     <!-- 允许程序振动 -->
     *     <uses-permission android:name="android.permission.VIBRATE" />
     *     <!-- 麦克风权限 -->
     *     <uses-permission android:name="android.permission.RECORD_AUDIO" />
     *     <!-- 相机权限 -->
     *     <uses-permission android:name="android.permission.CAMERA" />
     *
     *     <!-- 写入扩展存储权限-->
     *     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     *     <!-- 用于访问wifi网络信息-->
     *     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     *     <!-- 用于获取wifi的获取权限 -->
     *     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
     *
     *     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 允许程序在手机屏幕关闭后后台进程仍然运行 -->
     *     <uses-permission android:name="android.permission.WAKE_LOCK" />
     */
    private String[] permission  = {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.REORDER_TASKS
            , Manifest.permission.VIBRATE,Manifest.permission.RECORD_AUDIO,Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE
            ,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.CHANGE_WIFI_STATE,Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.WAKE_LOCK};
public static int REQUEST_CODE = 1000;
   private List<String> permissionList = new ArrayList<>();//这个存放不具备的权限名
    public void checkPermission(Activity activity){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){//在安卓6.0以上
            for (int i = 0; i < permission.length; i++) {
                //查询应用是否具备我们所需的权限
               if (ContextCompat.checkSelfPermission(activity,permission[i])!= PackageManager.PERMISSION_GRANTED){
                   //不具备的话
                    permissionList.add(permission[i]);
               }

            }
            if(permissionList.size()>0){
                requestPermission(activity);
            }
        }
    }

    private void requestPermission(Activity activity) {
        ActivityCompat.requestPermissions(activity, permissionList.toArray(new String[permissionList.size()]), REQUEST_CODE);
    }
}
